﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Drawing;
using System.IO;
using System.Globalization;

namespace IGGCore
{
    /// <summary>
    /// Manager of rss feeds
    /// </summary>
    public class NeyerParser
    {

        private TextInfo myTI = new CultureInfo("en-US", false).TextInfo;


        /// <summary>
        /// Return news from a source
        /// </summary>
        public List<News> GetNews(string url, int num)
        {
            List<News> newsList = new List<News>();
            News news = new News();
            news.Provider = "Rob Neyer";

            string raw = GetAllContent(url);
            raw = raw.Substring(raw.IndexOf("<div class=\"buzzer-header\">"));
            string link = raw.Substring(raw.IndexOf("<a href=\"") + "<a href=\"".Length);
            link = link.Substring(0, link.IndexOf(">"));
            link = link.Substring(0, link.IndexOf("\""));

            string title = raw.Substring(raw.IndexOf("<h3 class=\"buzzer-title\">") + "<h3 class=\"buzzer-title\">".Length);
            title = title.Substring(0, title.IndexOf("<"));

            // date
            string daterr = raw.Substring(raw.IndexOf("<span class=\"buzzer-pubdate\">") + "<span class=\"buzzer-pubdate\">".Length);
            daterr = daterr.Substring(0, daterr.IndexOf("<"));

            raw = raw = GetAllContent(link);
            raw = raw.Substring(raw.IndexOf("<head></head>"));


            raw = raw.Substring(0, raw.IndexOf("<div class=\"story-share-bar-top\">"));


            // TITLES
            news.Title = title;

            // DATE
            news.Date = daterr;

            // LINK
            news.Link = link;

            // CONTENT
            news.Content = raw;

            newsList.Add(news);
            return newsList;
        }

        /// <summary>
        /// Get the complete source of a specified website url
        /// </summary>
        private string GetAllContent(string url)
        {
            string transContent = null;
            StreamReader reader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                reader = new StreamReader(request.GetResponse().GetResponseStream());
                transContent = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return transContent;
        }


    }
}
